﻿<% @ LANGUAGE="VBSCRIPT" CODEPAGE="65001" %>
<% Option Explicit %>

<!-- #include file="../adovbs.inc" -->
<!-- #include file="checkfile.asp" -->

<%
    Dim id, ip
    Dim fromAddress, message, sender, toAddress
    Dim mailBody, mailSubject
    Dim checkMsg, result, logMsg
    
    'On Error resume Next

    id = Session("id")
    'id = Request.QueryString("code")

    checkMsg = checkfile(id)

    Response.ContentType = "text/plain"
    Response.CharSet = "utf-8"

    If checkMsg Then
        ip = Request.ServerVariables("REMOTE_ADDR")
        fromAddress = "webmaster@macao.communications.museum"
        message = Request.Form("message")
        sender = Request.Form("sender")    
        toAddress = Request.Form("address")
        mailSubject = Request.Form("subject")
        mailBody = setMailBody(message, sender)
        result = sendMail(fromAddress, toAddress, mailSubject, mailBody, id)
        If result = "SUCCESS" Then
            logMsg = insertLog(ip, id, "SUCCESS", toAddress, sender)
            If logMsg = "SUCCESS" Then
                Response.Write("TRUE")
            Else
                Response.Write(logMsg)
            End If
        Else
            logMsg = insertLog(ip, id, "FAILED:", toAddress, sender)
            If logMsg = "SUCCESS" Then
                Response.Write(result)
            Else
                Response.Write(logMsg)
            End If
        End If
    Else
        Response.Write("FILE_NOT_FOUND")
    End If

    Function setMailBody(Message, Sender)
        Dim m
        m = "<html xmlns='http://www.w3.org/1999/xhtml'>"
        m = m & "<head>"
        m = m & "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>"
        m = m & "<title></title>"
        m = m & "</head>"
        m = m & "<body>"
        m = m & "<p>" & Message & "</p>"
        m = m & "<p>" & Sender & "</p>"
        m = m & "</body>"
        m = m & "</html>"
        setMailBody = m
    End Function

    Function sendMail(FromAddress, ToAddress, Subject, HtmlBody, Id)
        Dim serverName, attachFileName, attachFileType
        Dim myMail
        Dim addachMsg

        On Error resume Next

        serverName = Request.ServerVariables("SERVER_NAME")
        attachFileName = "postcard.jpg"
        attachFileType = "image/jpeg"

        Set myMail = CreateObject("CDO.Message")
        addachMsg = addAttachment(myMail, Id, attachFileName, attachFileType)

        If addachMsg = "SUCCESS" Then
            myMail.Subject = Subject
            myMail.From = FromAddress 
            myMail.BodyPart.CharSet = "utf-8"
            myMail.To = ToAddress
            'MyMail.Bcc="webmaster@macao.communications.museum"
            myMail.HtmlBody = HtmlBody
            myMail.HTMLBodyPart.Charset = "utf-8"
            If serverName = "localhost" Then
                myMail.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.10.12"
            Else
                myMail.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.215.186"
            End If
            myMail.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            myMail.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            myMail.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
            'myMail.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
            'myMail.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "info@macao.communications.museum"
            'myMail.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "info0621"
            myMail.Configuration.Fields.Update
            myMail.Send
            If Err.number <> 0 Then
                sendMail = "MAIL_SEND_ERROR:" & Err.Source & "(" & Err.number & " - " & Err.Description & ")"
            Else
                sendMail = "SUCCESS"
            End If
        Else
            sendMail = addachMsg
        End If

        Set myMail = Nothing
    End Function

    Function addAttachment(Message, Id, FileName, MimeType)
        Const adTypeBinary = 1
        Dim path, sourceFile
        Dim oPart, oStreamIn, oStreamOut
        Dim size, i

        On Error resume Next

        path = Server.MapPath("/") & "\..\database\images\"
        SourceFile = path & Replace(Id,".","") & ".pc"

        Set oPart = Message.Attachments.Add
        oPart.ContentMediaType = MimeType & "; Name = """ & FileName & """"
        oPart.ContentTransferEncoding = "base64"
        oPart.Fields("urn:schemas:mailheader:content-disposition").Value = "attachment; FileName = """ & FileName & """"
        oPart.Fields.Update

        Set oStreamIn = Server.CreateObject("ADODB.Stream")
        oStreamIn.Open
        oStreamIn.Type = adTypeBinary
        oStreamIn.LoadFromFile sourceFile
        size = oStreamIn.Size
        oStreamIn.Position = size - 20

        Set oStreamOut = oPart.GetDecodedContentStream
        oStreamOut.Write oStreamIn.Read(20)
        oStreamIn.Position = 20
        For i = 21 To size - 20
            oStreamOut.Write oStreamIn.Read(1)
        Next
        oStreamIn.Position = 0
        oStreamOut.Write oStreamIn.Read(20)
        oStreamOut.Flush
    
        oStreamOut.Close
        oStreamIn.Close
        Set addAttachment = oPart

        If Err.number <> 0 Then
            addAttachment = "ATTACHMENT_ERROR:" & Err.Source & "(" & Err.number & " - " & Err.Description & ")"
        Else
            addAttachment = "SUCCESS"
        End If
    End Function

    Function insertLog(ip, id, status, toAddress, sender)
        Dim conn, cmd
        Dim dateTime

        On Error resume Next

        dateTime = FormatDateTime(Now(),0)

        Set conn = Server.CreateObject("ADODB.Connection")
        conn.Provider="Microsoft.Jet.OLEDB.4.0"
        conn.Open(Server.MapPath("/")+"\..\database\postcard.mdb")
    
        Set cmd = Server.CreateObject("ADODB.Command")
        cmd.ActiveConnection = conn
        cmd.CommandText = "INSERT INTO tblEmailLog(IP, Code,Status,Address,Sender,SendDateTime) VALUES(?,?,?,?,?,?)"
        cmd.CommandType = adCmdText
        cmd.Parameters.Append cmd.CreateParameter("@ip",adVarChar,adParamInput,50,0)
        cmd.Parameters.Append cmd.CreateParameter("@code",adVarChar,adParamInput,15,0)
        cmd.Parameters.Append cmd.CreateParameter("@status",adVarChar,adParamInput,100,0)
        cmd.Parameters.Append cmd.CreateParameter("@address",adVarChar,adParamInput,100,0)
        cmd.Parameters.Append cmd.CreateParameter("@sender",adVarChar,adParamInput,100,0)
        cmd.Parameters.Append cmd.CreateParameter("@datetime",adVarChar,adParamInput,50,0)
        cmd.Parameters("@ip") = ip
        cmd.Parameters("@code") = id
        cmd.Parameters("@status") = status
        cmd.Parameters("@address") = toAddress
        cmd.Parameters("@sender") = sender
        cmd.Parameters("@datetime") = dateTime
        
        cmd.Execute

        conn.Close

        If Err.number <> 0 Then
            insertLog = "DB_ERROR:" & Err.Source & "(" & Err.number & " - " & Err.Description & ")"
        Else
            insertLog = "SUCCESS"
        End If
    End Function
 %>